﻿using System;
using System.Collections.Generic;

namespace Poly2Tri
{
	// Token: 0x020003FD RID: 1021
	public class PolygonOperationContext
	{
		// Token: 0x17000400 RID: 1024
		// (get) Token: 0x06001C4B RID: 7243 RVA: 0x000B1BB4 File Offset: 0x000AFDB4
		public Point2DList Union
		{
			get
			{
				Point2DList point2DList = null;
				if (!this.mOutput.TryGetValue(1U, out point2DList))
				{
					point2DList = new Point2DList();
					this.mOutput.Add(1U, point2DList);
				}
				return point2DList;
			}
		}

		// Token: 0x17000401 RID: 1025
		// (get) Token: 0x06001C4C RID: 7244 RVA: 0x000B1BEC File Offset: 0x000AFDEC
		public Point2DList Intersect
		{
			get
			{
				Point2DList point2DList = null;
				if (!this.mOutput.TryGetValue(2U, out point2DList))
				{
					point2DList = new Point2DList();
					this.mOutput.Add(2U, point2DList);
				}
				return point2DList;
			}
		}

		// Token: 0x17000402 RID: 1026
		// (get) Token: 0x06001C4D RID: 7245 RVA: 0x000B1C24 File Offset: 0x000AFE24
		public Point2DList Subtract
		{
			get
			{
				Point2DList point2DList = null;
				if (!this.mOutput.TryGetValue(4U, out point2DList))
				{
					point2DList = new Point2DList();
					this.mOutput.Add(4U, point2DList);
				}
				return point2DList;
			}
		}

		// Token: 0x06001C4E RID: 7246 RVA: 0x000B1C5C File Offset: 0x000AFE5C
		public void Clear()
		{
			this.mOperations = PolygonUtil.PolyOperation.None;
			this.mOriginalPolygon1 = null;
			this.mOriginalPolygon2 = null;
			this.mPoly1 = null;
			this.mPoly2 = null;
			this.mIntersections = null;
			this.mStartingIndex = -1;
			this.mError = PolygonUtil.PolyUnionError.None;
			this.mPoly1VectorAngles = null;
			this.mPoly2VectorAngles = null;
			this.mOutput = new Dictionary<uint, Point2DList>();
		}

		// Token: 0x06001C4F RID: 7247 RVA: 0x000B1CBC File Offset: 0x000AFEBC
		public bool Init(PolygonUtil.PolyOperation operations, Point2DList polygon1, Point2DList polygon2)
		{
			this.Clear();
			this.mOperations = operations;
			this.mOriginalPolygon1 = polygon1;
			this.mOriginalPolygon2 = polygon2;
			this.mPoly1 = new Point2DList(polygon1);
			this.mPoly1.WindingOrder = Point2DList.WindingOrderType.CCW;
			this.mPoly2 = new Point2DList(polygon2);
			this.mPoly2.WindingOrder = Point2DList.WindingOrderType.CCW;
			if (!this.VerticesIntersect(this.mPoly1, this.mPoly2, out this.mIntersections))
			{
				this.mError = PolygonUtil.PolyUnionError.NoIntersections;
				return false;
			}
			int count = this.mIntersections.Count;
			for (int i = 0; i < count; i++)
			{
				for (int j = i + 1; j < count; j++)
				{
					if (this.mIntersections[i].EdgeOne.EdgeStart.Equals(this.mIntersections[j].EdgeOne.EdgeStart) && this.mIntersections[i].EdgeOne.EdgeEnd.Equals(this.mIntersections[j].EdgeOne.EdgeEnd))
					{
						this.mIntersections[j].EdgeOne.EdgeStart = this.mIntersections[i].IntersectionPoint;
					}
					if (this.mIntersections[i].EdgeTwo.EdgeStart.Equals(this.mIntersections[j].EdgeTwo.EdgeStart) && this.mIntersections[i].EdgeTwo.EdgeEnd.Equals(this.mIntersections[j].EdgeTwo.EdgeEnd))
					{
						this.mIntersections[j].EdgeTwo.EdgeStart = this.mIntersections[i].IntersectionPoint;
					}
				}
			}
			foreach (EdgeIntersectInfo edgeIntersectInfo in this.mIntersections)
			{
				if (!this.mPoly1.Contains(edgeIntersectInfo.IntersectionPoint))
				{
					this.mPoly1.Insert(this.mPoly1.IndexOf(edgeIntersectInfo.EdgeOne.EdgeStart) + 1, edgeIntersectInfo.IntersectionPoint);
				}
				if (!this.mPoly2.Contains(edgeIntersectInfo.IntersectionPoint))
				{
					this.mPoly2.Insert(this.mPoly2.IndexOf(edgeIntersectInfo.EdgeTwo.EdgeStart) + 1, edgeIntersectInfo.IntersectionPoint);
				}
			}
			this.mPoly1VectorAngles = new List<int>();
			for (int k = 0; k < this.mPoly2.Count; k++)
			{
				this.mPoly1VectorAngles.Add(-1);
			}
			this.mPoly2VectorAngles = new List<int>();
			for (int l = 0; l < this.mPoly1.Count; l++)
			{
				this.mPoly2VectorAngles.Add(-1);
			}
			int num = 0;
			for (;;)
			{
				bool flag = this.PointInPolygonAngle(this.mPoly1[num], this.mPoly2);
				this.mPoly2VectorAngles[num] = ((!flag) ? 0 : 1);
				if (flag)
				{
					break;
				}
				num = this.mPoly1.NextIndex(num);
				if (num == 0)
				{
					goto IL_351;
				}
			}
			this.mStartingIndex = num;
			IL_351:
			if (this.mStartingIndex == -1)
			{
				this.mError = PolygonUtil.PolyUnionError.Poly1InsidePoly2;
				return false;
			}
			return true;
		}

		// Token: 0x06001C50 RID: 7248 RVA: 0x000B2040 File Offset: 0x000B0240
		private bool VerticesIntersect(Point2DList polygon1, Point2DList polygon2, out List<EdgeIntersectInfo> intersections)
		{
			intersections = new List<EdgeIntersectInfo>();
			double epsilon = Math.Min(polygon1.Epsilon, polygon2.Epsilon);
			for (int i = 0; i < polygon1.Count; i++)
			{
				Point2D point2D = polygon1[i];
				Point2D point2D2 = polygon1[polygon1.NextIndex(i)];
				for (int j = 0; j < polygon2.Count; j++)
				{
					Point2D intersectionPoint = new Point2D();
					Point2D point2D3 = polygon2[j];
					Point2D point2D4 = polygon2[polygon2.NextIndex(j)];
					if (TriangulationUtil.LinesIntersect2D(point2D, point2D2, point2D3, point2D4, ref intersectionPoint, epsilon))
					{
						intersections.Add(new EdgeIntersectInfo(new Edge(point2D, point2D2), new Edge(point2D3, point2D4), intersectionPoint));
					}
				}
			}
			return intersections.Count > 0;
		}

		// Token: 0x06001C51 RID: 7249 RVA: 0x000B210C File Offset: 0x000B030C
		public bool PointInPolygonAngle(Point2D point, Point2DList polygon)
		{
			double num = 0.0;
			for (int i = 0; i < polygon.Count; i++)
			{
				Point2D p = polygon[i] - point;
				Point2D p2 = polygon[polygon.NextIndex(i)] - point;
				num += this.VectorAngle(p, p2);
			}
			return Math.Abs(num) >= 3.141592653589793;
		}

		// Token: 0x06001C52 RID: 7250 RVA: 0x000B2180 File Offset: 0x000B0380
		public double VectorAngle(Point2D p1, Point2D p2)
		{
			double num = Math.Atan2(p1.Y, p1.X);
			double num2 = Math.Atan2(p2.Y, p2.X);
			double num3;
			for (num3 = num2 - num; num3 > 3.141592653589793; num3 -= 6.283185307179586)
			{
			}
			while (num3 < -3.141592653589793)
			{
				num3 += 6.283185307179586;
			}
			return num3;
		}

		// Token: 0x04001D83 RID: 7555
		public PolygonUtil.PolyOperation mOperations;

		// Token: 0x04001D84 RID: 7556
		public Point2DList mOriginalPolygon1;

		// Token: 0x04001D85 RID: 7557
		public Point2DList mOriginalPolygon2;

		// Token: 0x04001D86 RID: 7558
		public Point2DList mPoly1;

		// Token: 0x04001D87 RID: 7559
		public Point2DList mPoly2;

		// Token: 0x04001D88 RID: 7560
		public List<EdgeIntersectInfo> mIntersections;

		// Token: 0x04001D89 RID: 7561
		public int mStartingIndex;

		// Token: 0x04001D8A RID: 7562
		public PolygonUtil.PolyUnionError mError;

		// Token: 0x04001D8B RID: 7563
		public List<int> mPoly1VectorAngles;

		// Token: 0x04001D8C RID: 7564
		public List<int> mPoly2VectorAngles;

		// Token: 0x04001D8D RID: 7565
		public Dictionary<uint, Point2DList> mOutput = new Dictionary<uint, Point2DList>();
	}
}
